#!/usr/bin/env node
/*
 * Copyright 2015-2016 Imply Data, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

var fs = require('fs');
var mkdirp = require('mkdirp');

if (process.argv.length !== 3 && process.argv.length !== 4) {
  console.log('Usage: mkmodel <model-name> <type?>');
  process.exit();
}

var name = process.argv[2];
if (!/^([a-z-])+$/.test(name)) {
  console.log('must be a hyphen case name');
  process.exit(1);
}

var type = process.argv[3] || 'common';
if (['common', 'server', 'client'].indexOf(type) === -1) {
  console.log('type must be one of common, server, client');
  process.exit(1);
}

var path = `./src/${type}/models/${name}/`;
mkdirp.sync(path);
console.log('Making path:', path);

var camelName = name.replace(/(^|-)[a-z]/g, (s) => s.replace('-', '').toUpperCase());

function writeFile(path, data) {
  try {
    console.log("Writing " + path);
    fs.writeFileSync(path, data, {
      flag: 'wx', // x = fail if file exists
      encoding: 'utf8'
    });
  } catch (error) {
    console.log("Skipping " + path);
  }
}

// Make the TypeScript file
writeFile(path + name + '.ts',
`/*
 * Copyright 2015-2016 Imply Data, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { BaseImmutable, Property, isInstanceOf } from 'immutable-class';
import { $, Expression } from 'plywood';

// I am: export * from './${name}/${name}';

export interface ${camelName}Value {

}

export interface ${camelName}JS {

}

export class ${camelName} extends BaseImmutable<${camelName}Value, ${camelName}JS> {

  static is${camelName}(candidate: any): candidate is ${camelName} {
    return isInstanceOf(candidate, ${camelName});
  }

  static fromJS(parameters: ${camelName}JS): ${camelName} {    
    return new ${camelName}(BaseImmutable.jsToValue(${camelName}.PROPERTIES, parameters));
  }

  static PROPERTIES: Property[] = [
    //{ name: 'blah' }
  ];
  
  //public blah: string;

  constructor(parameters: ${camelName}Value) {
    super(parameters);
  }

}
BaseImmutable.finalize(${camelName});
`);


// Make the TypeScript test file
writeFile(path + name + '.mocha.ts',
`/*
 * Copyright 2015-2016 Imply Data, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { expect } from 'chai';
import { testImmutableClass } from 'immutable-class-tester';

import { $, Expression } from 'plywood';
import { ${camelName} } from './${name}';

describe('${camelName}', () => {
  it('is an immutable class', () => {
    testImmutableClass(${camelName}, [

    ]);
  });

});
`);


// Make the TypeScript mock file
writeFile(path + name + '.mock.ts',
`/*
 * Copyright 2015-2016 Imply Data, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { ${camelName}, ${camelName}JS } from './${name}';

export class ${camelName}Mock {
  static blahJS(): ${camelName}JS {
    return {
      /* ... */
    };
  }

  static blah() {
    return ${camelName}.fromJS(${camelName}Mock.blahJS());
  }
}
`);
